home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
NetNews Offline 2
/
NetNews Offline Volume 2.iso
/
news
/
comp
/
std
/
c
/
284
< prev
next >
Wrap
Text File
|
1996-08-06
|
3KB
|
81 lines
Newsgroups: comp.std.c
Path: news.netins.net!duke!scottm
From: scottm@uis.com (Scott Miller)
Subject: Re: HELP!! Beginner's question...
Message-ID: <DM5LC6.M2n@uis.com>
Date: Fri, 2 Feb 1996 14:52:53 GMT
References: <Pine.OSF.3.91l.960131005708.7552A-100000@saul3.u.washington.edu>
Organization: Unix Integration Services
I compiled your program and ran it on our HP 700 series Unix machine,
entered 5 as the non-negative integer and 7 as the power, and got 78,125.
The program appears to be correct.
This could be a system dependant problem -- different systems may use
different sizes for int and long int. On your system, int may be a
16 bit number in which case it would not be large enough to store 78,125.
(2^16 = 65536, but remember he's using signed numbers so Max int is
smaller yet). Your instructor's advice to use long int sounds wise --
try it if you haven't already.
Scott Miller
Ramon Mariano Jr <rmariano@u.washington.edu> writes:
>Hello everybody!
>I'm a beginning C-programmer and for the past couple of days, I've been
>totally stuck in my program. The purpose of my program is to ask the user
>for an integer 'i' and raise it by a power 'n'. I've been succesful in
>computing 5 to the 6th power, 3 to the 4th power, and other small-sized
>equations. However, when I try to raise 5 to the 7th power, I get "12589"
>when it really should be "78125". My teacher told me that instead of using
>plain 'int', I should use 'long int'. I tried that, but it's still not
>working. If anyone can help me out here, I'd GREATLY appreciate it! I've
>included my code below... (by the way, I'm not allowed to use the <math.h>
>library)
>------------------------------------------------------------------------
>#include <stdio.h>
>
>int main(void)
>{
> int i, /* integer */
> n, /* power to raise integer */
> i_total, /* integer's total value*/
> count; /* keeps track of number of loops run */
> /* asks for an integer and stores it in 'i' */
> printf("Enter a non-negative integer: ");
> scanf("%d", &i);
> /* asks for a power and stores it in 'n' */
> printf("What power should we raise it to? ");
> scanf("%d", &n);
> /* if 'n' is negative, program will treat it as a '0' */
> if (n < 0){
> n = 0;
> }
>
> i_total = 1; /* gives i_total an initial total of '1' */
> for (count = 1; count <= n; count = count + 1){
> i_total = i_total * i;
> }
> /* prints out the inputs and result */
> printf("%d raised to the %dth power is %d\n\n", i, n, i_total);
> return(0);
>}
--
--
+-----------------------------------------------------------------+
| Scott A. Miller |
| Unix Integration Services scottm@uis.com |